package me.keiwu.admin.filter;

import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by kei on 29/11/2017.
 */
public class UserLoginFilter extends UsernamePasswordAuthenticationFilter {

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
            throws AuthenticationException {
        UsernamePasswordAuthenticationToken authRequest;
        try {
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            authRequest = new UsernamePasswordAuthenticationToken(username, password);
        } catch (Exception e) {
            authRequest = new UsernamePasswordAuthenticationToken("", "");
        }
        super.setDetails(request, authRequest);
        return super.getAuthenticationManager().authenticate(authRequest);
    }


}
